package com.example.administrator.daydayappmarket.software.category.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.software.category.activity.SoftwareCategorySecondActivity;
import com.example.administrator.daydayappmarket.software.category.adapter.SoftwareCategoryAdapter;
import com.example.administrator.daydayappmarket.software.category.model.SoftwareCategoryBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 */
public class SWCategoryFragment extends Fragment {


    @InjectView(R.id.fragment_software_category_gridView)
    GridView fragmentSoftwareCategoryGridView;
    @InjectView(R.id.fragment_software_category_emptyImageView)
    ImageView fragmentSoftwareCategoryEmptyImageView;
    @InjectView(R.id.fragment_software_category_emptyRelative)
    RelativeLayout fragmentSoftwareCategoryEmptyRelative;
    private List<SoftwareCategoryBean> data;
    private SoftwareCategoryAdapter adapter;
    private RequestQueue requestQueue;

    public SWCategoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = new ArrayList<>();
        requestQueue = Volley.newRequestQueue(getContext());
        StringRequest request = new StringRequest(Request.Method.GET, Contacts.SOFTWARE_CATEGORY, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {

                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                            SoftwareCategoryBean softwareCategoryBean = new SoftwareCategoryBean();
                            softwareCategoryBean.parseJson(jsonObject1);
                            data.add(softwareCategoryBean);
                        }

                        adapter.notifyDataSetChanged();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put(Contacts.HEADER_KEY, Contacts.HEADER_VALUES);

                return map;
            }
        };
        request.setTag("request");
        requestQueue.add(request);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_swcategory, container, false);
        ButterKnife.inject(this, view);
        adapter = new SoftwareCategoryAdapter(getContext(), data);
        fragmentSoftwareCategoryGridView.setAdapter(adapter);

        Glide.with(getContext()).load(R.drawable.cat_loading_transparent).into(fragmentSoftwareCategoryEmptyImageView);
        fragmentSoftwareCategoryGridView.setEmptyView(fragmentSoftwareCategoryEmptyRelative);

        fragmentSoftwareCategoryGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String id = data.get(i).getId();
                Intent intent=new Intent(getContext(), SoftwareCategorySecondActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("title",data.get(i).getTitle());
                startActivity(intent);
            }
        });


        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
        requestQueue.cancelAll("request");
    }
}
